package com.kuaiyin.controller.request;


import com.kuaiyin.dto.EnvironmentDTO;
import com.kuaiyin.entity.Environment;
import com.kuaiyin.result.Result;
import com.kuaiyin.service.IEnvironmentService;
import com.kuaiyin.utils.PageResult;
import com.kuaiyin.vo.EnvironmentVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 全局变量表 前端控制器
 * </p>
 *
 * @author ng
 * @since 2025-04-27
 */
@Slf4j
@Tag(name = "全局变量管理")
@RestController
@RequestMapping("/environment")
public class EnvironmentController {
    @Autowired
    private IEnvironmentService environmentService;

    @Operation(summary = "新增全局变量")
    @PostMapping()
    public Result<String> saveEnvironment(@Valid @RequestBody EnvironmentDTO environmentDTO) {
        environmentService.saveEnvironment(environmentDTO);
        return Result.success("新增成功");
    }

    @Operation(summary = "更新全局变量")
    @PutMapping("/{id}")
    public Result<String> updateEnvironment(@PathVariable Long id, @Valid @RequestBody EnvironmentDTO environmentDTO) {
        environmentService.updateEnvironment(id, environmentDTO);
        return Result.success("更新成功");
    }

    @Operation(summary = "删除全局变量")
    @DeleteMapping("/{id}")
    public Result<String> deleteEnvironment(@PathVariable Long id) {
        environmentService.removeById(id);
        return Result.success("删除成功");
    }
    @Operation(summary = "查询全局变量详情")
    @GetMapping("/{id}")
    public Result<EnvironmentVO> getEnvironment(@PathVariable Long id) {
        EnvironmentVO environmentVO = environmentService.getById(id);
        return Result.success(environmentVO);
    }

    @Operation(summary = "全局变量列表")
    @GetMapping("/all")
    public Result<List<EnvironmentVO>> getEnvironmentList() {
        List<EnvironmentVO> environmentList = environmentService.getEnvironmentList();
        return Result.success(environmentList);
    }

    @Operation(summary = "分页查询全局变量列表")
    @GetMapping("/search")
    public Result<PageResult<EnvironmentVO>> getEnvironment(
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String environmentType,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        PageResult<EnvironmentVO> environmentPage = environmentService.getEnvironmentPage(
                page, size, wrapper -> {
                    if (StringUtils.hasText(name)) {
                        wrapper.like(Environment::getVarName, name);
                    }
                    if (StringUtils.hasText(environmentType)) {
                        wrapper.eq(Environment::getEnvironmentType, environmentType);
                    }
                    wrapper.orderByDesc(Environment::getId);
                }
        );
        return Result.success(environmentPage);
    }
}
